--Heights for skyboxes
local space_low = 5000
local space_high = 5999
local redsky_low = 6000
local redsky_high = 7950

local player_list = {} -- Holds name of skybox showing for each player
local timer = 0

minetest.register_globalstep(function(dtime)
	timer = timer + dtime
	if timer < 2 then
		return
	end
	timer = 0
	for _, player in pairs(minetest.get_connected_players()) do

		local name = player:get_player_name()
		local pos = player:get_pos()
		local current = player_list[name] or ""

		-- Earth
		if pos.y < space_low and current ~= "earth" then
         local sl = {}
      	sl.name = "plain_with_clouds_sky"
         sl.sky_data = {
      		sky_color = {
      			day_sky = "#8cbafa",
      			day_horizon = "#9bc1f0",
      			dawn_sky = "#b4bafa",
      			dawn_horizon = "#d8bae1",
      			night_sky = "#006aff",
      			night_horizon = "#4090ff",
      			indoors = "#646464",
      			fog_sun_tint = "#E0901F",
      			fog_moon_tint = "#6E9A65"
      		},
            clouds = true
      	}
      	sl.clouds_data = {
      		gradient_colors = {
      			{ r = 34, g = 204, b = 170 },
      			{ r = 105, g = 208, b = 37 },
      			{ r = 170, g = 204, b = 34 },
      			{ r = 208, g = 195, b = 16 },
      			{ r = 204, g = 187, b = 51 },
      			{ r = 254, g = 174, b = 45 },
      			{ r = 255, g = 68, b = 34 },
      			{ r = 255, g = 102, b = 68 },
      			{ r = 255, g = 153, b = 51 },
      			{ r = 254, g = 174, b = 45 },
      			{ r = 59, g = 12, b = 189 }
      		},
      		speed = { x = 2, z = -2 }
      	}
      	sl.sun_data = {
      		visible = true,
      		sunrise_visible = true
      	}
      	skylayer.add_layer(name, sl)
			player_list[name] = "earth"
			if otherworlds.settings.gravity.enable then
				player:set_physics_override({gravity = 1})
			end

		-- Outerspace
		elseif pos.y > space_low and pos.y < space_high and current ~= "space" then
         local sl = {}
      	sl.name = "other_worlds:spaceskybox"
      	sl.moon_data = {
      		visible = false,
      		scale = 0.8
      	}
         sl.sun_data = {
            visible = false,
            scale = 0.8,
            sunrise_visible = false
         }
         sl.stars_data = {
            visible = false,
            count = 0,
         }
         sl.sky_data = {
            type = "skybox",
            textures = {
               "sky_pos_z.png",
            	"sky_neg_z.png^[transformR180",
            	"sky_neg_y.png^[transformR270",
            	"sky_pos_y.png^[transformR270",
            	"sky_pos_x.png^[transformR270",
            	"sky_neg_x.png^[transformR90",
            },
            sky_color = {
      			day_sky = "#000000",
      			day_horizon = "#000000",
      			dawn_sky = "#000000",
      			dawn_horizon = "#000000",
      			night_sky = "#000000",
      			night_horizon = "#000000",
      			indoors = "#000000",
      			fog_sun_tint = "#000000",
      			fog_moon_tint = "#000000"
      		}
         }
         skylayer.add_layer(name, sl)
			player_list[name] = "space"
			if otherworlds.settings.gravity.enable then
				player:set_physics_override({gravity = 0.4})
			end

		-- Redsky
		elseif pos.y > redsky_low and pos.y < redsky_high and current ~= "redsky" then
         local sl = {}
      	sl.name = "other_worlds:redskybox"
      	sl.moon_data = {
      		visible = false,
      		scale = 0.8
      	}
         sl.sun_data = {
            visible = false,
            scale = 0.8,
            sunrise_visible = false
         }
         sl.stars_data = {
            visible = false,
            count = 0,
         }
         sl.sky_data = {
            type = "skybox",
            textures = {
               "sky_pos_z.png^[colorize:#99000050",
            	"sky_neg_z.png^[transformR180^[colorize:#99000050",
            	"sky_neg_y.png^[transformR270^[colorize:#99000050",
            	"sky_pos_y.png^[transformR270^[colorize:#99000050",
            	"sky_pos_x.png^[transformR270^[colorize:#99000050",
            	"sky_neg_x.png^[transformR90^[colorize:#99000050",
            },
            sky_color = {
      			day_sky = "#000000",
      			day_horizon = "#000000",
      			dawn_sky = "#000000",
      			dawn_horizon = "#000000",
      			night_sky = "#000000",
      			night_horizon = "#000000",
      			indoors = "#000000",
      			fog_sun_tint = "#000000",
      			fog_moon_tint = "#000000"
      		}
         }
         skylayer.add_layer(name, sl)
			player_list[name] = "redsky"
			if otherworlds.settings.gravity.enable then
				player:set_physics_override({gravity = 0.2})
			end

		-- Everything else (blackness)
		elseif pos.y > redsky_high and current ~= "heavens" then
         local sl = {}
      	sl.name = "plain_with_clouds_sky"
         sl.sky_data = {
      		sky_color = {
      			day_sky = "#8cbafa",
      			day_horizon = "#9bc1f0",
      			dawn_sky = "#b4bafa",
      			dawn_horizon = "#d8bae1",
      			night_sky = "#006aff",
      			night_horizon = "#4090ff",
      			indoors = "#646464",
      			fog_sun_tint = "#E0901F",
      			fog_moon_tint = "#6E9A65"
      		},
            clouds = true
      	}
      	sl.clouds_data = {
      		gradient_colors = {
      			{ r = 34, g = 204, b = 170 },
      			{ r = 105, g = 208, b = 37 },
      			{ r = 170, g = 204, b = 34 },
      			{ r = 208, g = 195, b = 16 },
      			{ r = 204, g = 187, b = 51 },
      			{ r = 254, g = 174, b = 45 },
      			{ r = 255, g = 68, b = 34 },
      			{ r = 255, g = 102, b = 68 },
      			{ r = 255, g = 153, b = 51 },
      			{ r = 254, g = 174, b = 45 },
      			{ r = 59, g = 12, b = 189 }
      		},
      		speed = { x = 2, z = -2 }
      	}
      	sl.sun_data = {
      		visible = true,
      		sunrise_visible = true
      	}
      	skylayer.add_layer(name, sl)
			player_list[name] = "heavens"
			if otherworlds.settings.gravity.enable then
				player:set_physics_override({gravity = 0.1})
			end
		end
	end
end)

minetest.register_on_leaveplayer(function(player)
	local name = player:get_player_name()
	player_list[name] = nil
	if otherworlds.settings.gravity.enable then
		player:set_physics_override({gravity = 1})
	end
end)
